/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bm.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.*;
import bm.forms.LoginForm;

/**
 *
 * @author chaosmeleon
 */
public class Login extends Action {

    private String usuario[];

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //System.out.println("adduser action file");
        LoginForm loginForm = (LoginForm) form;
        String login = loginForm.getLogin();
        String pass = loginForm.getPass();

        if (this.verificarUser(login, pass)[0] != null){
            request.setAttribute("login", login);
            if (usuario[2].equals("admin")){

                return mapping.findForward("sistemaAdministracion");
            } else

                return mapping.findForward("index");
        } else {
            System.out.println("llego a error");
            return mapping.findForward("error");
        }
        
    }

    private String[] verificarUser(String login, String pass){
        
        //User user;
        ConnectBD.connectionBD();
        usuario = ConnectBD.loginBD("SELECT login, clave, tipo FROM usuario WHERE login='"+ login +"' AND clave='"+ pass +"';");
        ConnectBD.disconnectBD();
        System.out.println("Login:"+usuario[0]+", pass:"+usuario[1]+", status:"+usuario[2]);
        return usuario;

    }
}
